﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;

namespace EasyBackup.Utils
{
    static class Schedule
    {        
        private static List<string[]> _schedule = new List<string[]>();

        public static void CheckFile()
        {
            if (!File.Exists(App.SchedulePath))
            {
                string[] text = { "#Fill in this file according to the following format.",
                                  "#After changing the file, restart application (application read schedule at startup).",
                                  "#Lines starting with # are ignored.",                             
                                  Environment.NewLine,
                                  "#From: //networkFolder",
                                  "#    To: //backupFolder",
                                  "#    When: monday 22:30",                                  
                                  "",
                                  "#    To: //backupFolder2",
                                  "#    When: even week Thursday 23:00",                                  
                                  "",
                                 @"#    To: D:\Backups\backupFolder3",
                                  "#    When: odd week thursday 23:00",                                  
                                  "",
                                 @"#    To: F:\Backup",
                                  "#    When: everyday 23:00"                                  
                                };

                File.WriteAllLines(App.SchedulePath, text);
            }
        }

        public static void Get()
        {           
            string[] fileLines = File.ReadAllLines(App.SchedulePath);            

            string from = "";
            string to = "";            
            string whenDay = "";
            string whenWeek = "";
            string whenTime;

            foreach (string fileLine in fileLines)
            {
                string s = fileLine.Trim().ToLower();
                if (string.IsNullOrEmpty(s) || s.StartsWith("#") || !(s.Contains("from:") || s.Contains("to:") || s.Contains("when:")))                                    
                    continue;

                if (s.Contains("from:"))
                {
                    from = s.Replace("from:", "").Trim();                                                            
                }

                if (s.Contains("to:"))
                {
                    to = s.Replace("to:", "").Trim();
                }

                if (s.Contains("when:"))
                {
                    s = s.Replace("when:", "").Trim();

                    if (s.Contains("odd week"))
                    {
                        whenWeek = "odd week";
                        s = s.Replace("odd week", "").Trim();
                    }
                    else if (s.Contains("even week"))
                    {
                        whenWeek = "even week";
                        s = s.Replace("even week", "").Trim();
                    }                    
                    else
                    {
                        whenWeek = "every";
                    }

                    whenTime = "";
                    for (int i = 0; i < s.Length; i++)
                    {
                        if (char.IsDigit(s[i]))
                        {                            
                            while (i < s.Length && (char.IsDigit(s[i]) || s[i] == ':'))
                            {
                                whenTime += s[i];
                                i++;
                            }                            
                        }
                    }

                    whenDay = s.Replace(whenTime, "").Trim();
                                        
                    _schedule.Add(new [] {whenTime, whenDay, whenWeek, to, from});                    
                }
            }
        }

        public static void Check(DateTime now)
        {            
            string nowTime = now.ToString("HH:mm");
            string nowTime2 = now.ToString("H:mm");        
            string nowDay = now.ToString("dddd", new CultureInfo("en-US")).ToLower();
            string nowWeek = Period.IsEvenWeek(now) ? "even week" : "odd week";

            foreach (string[] ss in _schedule)
            {
                if ((ss[0] == nowTime || ss[0] == nowTime2) && (ss[1] == nowDay || ss[1] == "everyday") && (nowWeek == ss[2] || ss[2] == "every"))
                {
                    Task.Run(() => Backup.Folder(ss[4], ss[3], nowTime));
                }
            }            
        }
    }
}
